package org.locker.service.impl;

import org.locker.common.core.utils.MapstructUtils;
import org.locker.common.core.utils.StringUtils;
import org.locker.common.mybatis.core.page.TableDataInfo;
import org.locker.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.locker.service.ICommonService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.locker.domain.bo.CommonBo;
import org.locker.domain.vo.CommonVo;
import org.locker.domain.Common;
import org.locker.mapper.CommonMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 品牌信息Service业务层处理
 *
 * @author winnie
 * @date 2023-10-11
 */
@RequiredArgsConstructor
@Service
public class CommonServiceImpl  extends ServiceImpl<CommonMapper,Common>  implements ICommonService {

    private final CommonMapper baseMapper;

    /**
     * 查询品牌信息
     */
    @Override
    public CommonVo queryById(Long commonId){
        return baseMapper.selectVoById(commonId);
    }

    /**
     * 查询品牌信息列表
     */
    @Override
    public TableDataInfo<CommonVo> queryPageList(CommonBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Common> lqw = buildQueryWrapper(bo);
        Page<CommonVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询品牌信息列表
     */
    @Override
    public List<CommonVo> queryList(CommonBo bo) {
        LambdaQueryWrapper<Common> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Common> buildQueryWrapper(CommonBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Common> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getBrandName()), Common::getBrandName, bo.getBrandName());
        lqw.like(StringUtils.isNotBlank(bo.getEnglishName()), Common::getEnglishName, bo.getEnglishName());
        lqw.eq(StringUtils.isNotBlank(bo.getServicePhone()), Common::getServicePhone, bo.getServicePhone());
        lqw.eq(StringUtils.isNotBlank(bo.getBrandDesc()), Common::getBrandDesc, bo.getBrandDesc());
        return lqw;
    }

    /**
     * 新增品牌信息
     */
    @Override
    public Boolean insertByBo(CommonBo bo) {
        Common add = MapstructUtils.convert(bo, Common.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCommonId(add.getCommonId());
        }
        return flag;
    }

    /**
     * 修改品牌信息
     */
    @Override
    public Boolean updateByBo(CommonBo bo) {
        Common update = MapstructUtils.convert(bo, Common.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Common entity){
        //做一些数据校验,如唯一约束
    }

    /**
     * 批量删除品牌信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
